{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from tqdm import tqdm_notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = {}\n",
    "keys = ['train', 'test']\n",
    "for k in keys :\n",
    "    df[k] = pd.read_csv('' + k + '.csv', header=None)\n",
    "    df[k] = df[k][df[k][0].isin([1, 3])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from Transparency.preprocess.vectorizer import cleaner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for k in keys :\n",
    "    texts = list(df[k][2])\n",
    "    for i in tqdm_notebook(range(len(texts))) :\n",
    "        texts[i] = cleaner(texts[i])\n",
    "    df[k]['text'] = texts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for k in keys :\n",
    "    df[k][0] = [1 if (x == 3) else 0 for x in list(df[k][0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df_texts = []\n",
    "df_labels = []\n",
    "df_exp_splits = []\n",
    "\n",
    "for key in ['train', 'test'] :\n",
    "    df_texts += list(df[key]['text'])\n",
    "    df_labels += list(df[key][0])\n",
    "    df_exp_splits += [key] * len(list(df[key]['text']))\n",
    "    \n",
    "df = pd.DataFrame({'text' : df_texts, 'label' : df_labels, 'exp_split' : df_exp_splits})\n",
    "df.to_csv('agnews_dataset.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('agnews_dataset.csv')\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "train_idx, dev_idx = train_test_split(df.index[df.exp_split == 'train'], test_size=0.15, random_state=16377)\n",
    "df.loc[dev_idx, 'exp_split'] = 'dev'\n",
    "df.to_csv('agnews_dataset_split.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vocabulary size :  13715\n",
      "Found 12226 words in model out of 13715\n"
     ]
    }
   ],
   "source": [
    "%run \"../preprocess_data_BC.py\" --data_file agnews_dataset_split.csv --output_file ./vec_agnews.p --word_vectors_type fasttext.simple.300d --min_df 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
